package com.xauto.pioneer.appuser.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.mine.user.EditMobileActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.MatcherUtil;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.util.ToastUtil;

/**
 * Created by Lemon on Tue 25 Apr 15:28
 * Desc:修改手机号 对话框
 */
public class ModifyMobileDialog extends Dialog {

    /** 标题 | 标题+手机号*/
    private TextView tvDialogTitle;
    /** 输入需要修改的目标手机号 | 验证码*/
    private EditText etDialogMobile;
    /** 发送验证码 | 验证码倒计时*/
    private TextView tvDialogSendVerCode;
    /** 下一步 | 确认*/
    private TextView tvDialogConfirm;
    /** 上一步 | 不显示*/
    private TextView tvDialogPre;

    /** 0-第一步输入手机号 | 1-第二步发送验证码 | 2-第三步输入验证码,提交*/
    private int currentStep = 0;
    private String editMobile = "";

    /***
     * 提示对话框
     * @param context 上下文
     */
    public ModifyMobileDialog(Context context) {
        super(context, R.style.LoadingDialogTheme);
        init(context);
    }

    private void init(final Context context) {
        setContentView(R.layout.dialog_modify_mobile);
        tvDialogTitle = (TextView) findViewById(R.id.tvDialogTitle);
        etDialogMobile = (EditText) findViewById(R.id.etDialogMobile);
        tvDialogSendVerCode = (TextView) findViewById(R.id.tvDialogSendVerCode);
        tvDialogPre = (TextView) findViewById(R.id.tvDialogPre);
        tvDialogConfirm = (TextView) findViewById(R.id.tvDialogConfirm);

        tvDialogConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentStep == 0) { //输入手机号
                    if (StringUtils.isBlank(etDialogMobile.getText().toString())) {
                        ToastUtil.show(context, R.string.mobile_is_not_empty);
                        return;
                    }
                    if (!MatcherUtil.Matcher(Constants.MATCHER_MOBILE, etDialogMobile.getText().toString())) {
                        ToastUtil.show(context, R.string.mobile_is_error);
                        return;
                    }
                    editMobile = etDialogMobile.getText().toString();
                    tvDialogTitle.setText("更改手机" + editMobile);
                    tvDialogConfirm.setText(R.string.confirm);
                    etDialogMobile.setText("");
                    etDialogMobile.setHint(R.string.login_input_ver_code);
                    tvDialogPre.setVisibility(View.VISIBLE);
                    tvDialogSendVerCode.setVisibility(View.VISIBLE);
                    currentStep = 1;
                    return;
                }
                if (currentStep == 1) { // 输入验证码-提交
                    if (StringUtils.isBlank(etDialogMobile.getText().toString())) {
                        ToastUtil.show(context, R.string.code_is_not_empty);
                        return;
                    }
                    if (!etDialogMobile.getText().toString().equals(verify_code)) {
                        ToastUtil.show(context, R.string.code_is_error);
                        return;
                    }
                    //todo 提交修改手机号
                    ((EditMobileActivity)context).editMobile(editMobile, code);
                    dismiss();
                }
            }
        });

        tvDialogPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentStep = 0;
                tvDialogTitle.setText(R.string.edit_mobile);
                tvDialogConfirm.setText(R.string.next);
                etDialogMobile.setText("");
                etDialogMobile.setHint("请输入目标手机号码");
                tvDialogPre.setVisibility(View.INVISIBLE);
                tvDialogSendVerCode.setVisibility(View.INVISIBLE);
                verify_code = "";
                code = "";
                min = 0;
            }
        });

        tvDialogSendVerCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送验证码
                if(!StringUtils.isBlank(editMobile)) {
                    loadValidMobile();
                }
            }
        });

        setCanceledOnTouchOutside(true);
        setCancelable(true);
    }


       void loadValidMobile() {
        new LHttpLib().validateMemberInfo(getContext(), Constants.TYPE_REG_LOGIN_MOBILE,
                editMobile, new BaseLHttpHandler(getContext(), true, false) {

                    @Override
                    public void onSuccess(JSONStatus jsonStatus) {
                        super.onSuccess(jsonStatus);
                        min = 60;
                        timeRunnable.run();
                        loadGetVerifyCode(getContext());
                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo, String strMsg) {
                        super.onFailure(t, errorNo, strMsg);
                        ToastUtil.showShort(getContext(), strMsg);
                        min = 0;
                        dismiss();
                    }
                });
    }

    /** 验证码，用于验证客户是否输入正确*/
    private String verify_code;
    /** 生成的CODE,用于在提交回传服务器验证码*/
    private String code;

    void loadGetVerifyCode(final Context mContext) {
        new LHttpLib().getVerifyCode(mContext, editMobile, new BaseLHttpHandler(mContext, true, false) {

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                verify_code = jsonStatus.data.optString("verify_code");
                code = jsonStatus.data.optString("code");
                ToastUtil.show(mContext, R.string.send_code_success);
                currentStep = 1;
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                ToastUtil.showShort(mContext, strMsg);
                min = 0;
            }
        });
    }


    Handler handler = new Handler(){};
    int min = 60;
    /** 倒计时*/
    Runnable timeRunnable = new Runnable() {
        @Override
        public void run() {
            if(min > 0){
                min--;
                tvDialogSendVerCode.setEnabled(false);
                tvDialogSendVerCode.setText(min + "s");
                handler.postDelayed(timeRunnable, 1000);
            }else{
                tvDialogSendVerCode.setText(R.string.login_send_ver_code);
                tvDialogSendVerCode.setEnabled(true);
            }
        }
    };
}
